#!/usr/bin/env bash
#################
stop_vnc_main() {
    tmoe_vnc_env
    rm_vnc_lock
    pkill_vnc
    check_wsl
    case "$1" in
    -no-stop-dbus)
        stopx11vnc
        ;;
    -no-stop-x11vnc) ;;
    -x11)
        stop_dbus_daemon
        stopx11vnc
        ;;
    -h | --h | --help | -help)
        cat <<-'EOF'
			-no-stop-dbus    --不停止dbus-daemon
			-no-stop-x11vnc  --不停止x11vnc
            -x11              --停止x11vnc
			-h               --获取帮助信息
		EOF
        ;;
    *)
        printf "%s\n" "${GREEN}Stopping ${YELLOW}vnc server${RESET} ..."
        stopx11vnc 2>/dev/null
        stop_dbus_daemon
        #stopx11vnc
        vnc_server_has_been_stopped
        ;;
    esac
}
############
check_wsl() {
    if [ "$(uname -r | cut -d '-' -f 3)" = "Microsoft" ] || [ "$(uname -r | cut -d '-' -f 2)" = "microsoft" ]; then
        export TMOE_CHROOT=true
        export TMOE_WSL=true
    elif grep -Eq '^[^#]*TMOE_CHROOT=.*true' /etc/environment 2>/dev/null; then
        export TMOE_CHROOT=true
    fi
}
###########
stopx11vnc() {
    pkill Xvfb
    rm -fv /tmp/.X233-lock /tmp/.X11-unix/X233 2>/dev/null
}
##########
tmoe_vnc_env() {
    export USER="$(whoami)"
    #CURRENT_PORT=$(sed -n p /usr/local/bin/startvnc | grep '\-geometry' | awk -F ' ' '$0=$NF' | cut -d ':' -f 2 | tail -n 1)
    CURRENT_PORT=$(grep 'VNC_DISPLAY=' /usr/local/bin/startvnc | head -n 1 | awk 'BEGIN{FS="="} {print $2}' | cut -d '"' -f 2)
    RED=$(printf '\033[31m')
    GREEN=$(printf '\033[32m')
    YELLOW=$(printf '\033[33m')
    BLUE=$(printf '\033[34m')
    PURPLE=$(printf '\033[35m')
    CYAN=$(printf '\033[36m')
    RESET=$(printf '\033[m')
    BOLD=$(printf '\033[1m')
}
###########
rm_vnc_lock() {
    [[ $(command -v vncsession) ]] || vncserver -kill :${CURRENT_PORT} 2>/dev/null
    rm -vf /tmp/.X${CURRENT_PORT}-lock 2>/dev/null
    rm -vf /tmp/.X11-unix/X${CURRENT_PORT} 2>/dev/null
}
#############
rm_dbus_pid_file() {
    if [ $(command -v sudo) ]; then
        sudo rm -vf /run/dbus/pid /var/run/dbus/pid /var/run/dbus/messagebus.pid /run/messagebus.pid /var/run/dbus/messagebus.pid /var/run/messagebus.pid 2>/dev/null
    else
        su -c "rm -vf /run/dbus/pid /var/run/dbus/pid /var/run/dbus/messagebus.pid /run/messagebus.pid /var/run/dbus/messagebus.pid /var/run/messagebus.pid 2>/dev/null"
    fi
}
############
stop_dbus_daemon() {
    case ${TMOE_CHROOT} in
    true)
        if [ -e "/var/run/dbus/pid" ]; then
            DBUS_PID=$(sed -n p /var/run/dbus/pid)
            printf "%s\n" "kill -9 ${DBUS_PID}"
            sudo kill -9 ${DBUS_PID} || kill -9 ${DBUS_PID}
            #此处不要用su -c "kill xxx"
            rm_dbus_pid_file
        fi
        ;;
    esac
    #pkill dbus
}
##########
pkill_vnc() {
    pkill Xtightvnc
    pkill Xtigervnc
    pkill Xvnc
    #pkill vncsession
    pkill websockify
}
#############
vnc_server_has_been_stopped() {
    if [ ! "$(pgrep Xvnc)" ]; then
        case ${LANG} in
        zh_*UTF-8)
            printf "%s\n" "已${PURPLE}停止${RESET}${YELLOW}VNC服务${RESET}！"
            printf "%s\n" "${YELLOW}VNC server${RESET} has been ${PURPLE}terminated${RESET}."
            ;;
        *) printf "%s\n" "${YELLOW}VNC server${RESET} has been ${PURPLE}terminated${RESET}." ;;
        esac
    fi
}
#################
stop_vnc_main "$@"
